<?php
/* Kewego Api PHP Client
 * version: 1.0
 * release date: 2008/06/11
 */

class KewegoClient
{
	private $url;
	private $xml;
	private $token;
	private $login_url;
	private $upload_url;
	private $api_url;
	public $appKey;
	public $appToken;

	function __construct($appToken='')
 	{
		$this->login_url 		= 'login.kewego.com';
		$this->upload_url 		= 'upload.kewego.com';
		$this->platform_url     = 'www.kewego.fr';
		$this->api_url          = 'api.kewego.com';
		$this->xml  	= '';
 	    $this->token    = false;
 	    $this->appKey   = $appToken;
	}

	function Call($api_url, $data_array='')
	{
		$this->xml 	= '';
		$this->url	= $api_url;

		if (extension_loaded('curl'))
		{
			$parsed_url = parse_url($api_url);

			$ch         = curl_init($parsed_url['scheme'].'://'.$parsed_url['host'].$parsed_url['path']);

			curl_setopt($ch, CURLOPT_HEADER, false);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_POST, true);

			$post_data = array();
			if (isset($parsed_url['query']))
			{
				$query_arg = explode('&',$parsed_url['query']);
				foreach($query_arg as $key => $arg)
				{
					$detail_arg = explode('=', $arg);
					$post_data[$detail_arg[0]] = $detail_arg[1];
				}
			}
			if ($this->token != false)
			{
			    $post_data['token'] = $this->token;
			}
			if ($this->appToken != '')
			{
			    $post_data['appToken'] = $this->appToken;
			}
			if ($data_array != '')
			{
			    //$post_data['datas'] = $datas;
				foreach($data_array as $data_key => $data)
				{
				    $post_data[$data_key] = $data;
				}
			}

 			curl_setopt($ch, CURLOPT_POSTFIELDS, substr($this->data_encode($post_data), 0, -1));
// 			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

			//debug: echo '<br />'.$api_url;

			$this->xml	= curl_exec($ch);

//			var_dump(curl_error($ch));

			curl_close($ch);
		}
		else
		{
		    echo "<hr />You need Curl Extension installed<hr />";
		    return false;
		}
	}

	function getResponse()
	{
		return $this->xml;
	}

	function getMessage()
	{
  		if ($response = $this->getResponse())
		{
			$xml_message = simplexml_load_string($response);
			$message = $xml_message->message;
			return (string)$message;
		}
		else
		{
		    return false;
		}
	}

	function loginUser($username, $password, $site_url)
	{

		// Appel a faire en HTTPS pour des raisons de sÃ©curitÃ©
		$url = 'http://'.$this->login_url.'/api/getAuthToken/?username='.urlencode($username).'&password='.urlencode($password).'&site_url='.$site_url;
		$this->Call($url);

		$xml_message = simplexml_load_string($this->getResponse());

		if (isset($xml_message->message->token))
		{
			$this->token = (string)$xml_message->message->token;
			return true;
		}
		else
		{
			return false;
		}
	}

	function setAppToken($appKey)
	{
		$url = 'http://'.$this->api_url.'/app/getToken/?appKey='.$appKey;
		//debug: echo $url.'<br />';
		$this->Call($url);

		//var_dump($this->getResponse());

		$xml_message = simplexml_load_string($this->getResponse());
		if (isset($xml_message->message->appToken))
		{
		    $this->appToken = (string)$xml_message->message->appToken;
		    return true;
		}
		else return false;
	}

	function getPlayerAppToken($appKey)
	{
		$url = 'http://'.$this->api_url.'/app/getPlayerAppToken/?playerKey='.$appKey;
		echo $url.'<br />';
		$this->Call($url);

		//var_dump($this->getResponse());

		$xml_message = simplexml_load_string($this->getResponse());
		if (isset($xml_message->message->player_app_token))
		{
		    $this->appToken = (string)$xml_message->message->player_app_token;

		    return true;
		}
		else
		{
		    return false;
		}
	}

	function upload($file, $xml_data, $site_url, $upload_key='')
	{
		$response  = '';

		$ch         = curl_init('http://'.$this->upload_url.'/api/uploadVideo/index.php?X-Progress-ID='.$upload_key);

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$data       = array(
						'file' 		=> '@'.$file,
						'xml_data' 	=> $xml_data,
						'token' 	=> $this->token,
						'site_url' 	=> $site_url
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passé correctement
		$response	= curl_exec($ch);

		echo curl_error($ch);

		curl_close($ch);

		return $response;
	}

	function uploadImage($file)
	{
		$response  = '';

		$ch         = curl_init('http://'.$this->upload_url.'/api/uploadImage/index.php');

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$data       = array(
						'file' 		=> '@'.$file,
						'token' 	=> $this->token,
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passÃ© correctement
		$response	= curl_exec($ch);

		curl_close($ch);

		return $response;
	}

	function setDetails($sig, $datas, $token)
	{
		$response  = '';

		$ch         = curl_init('http://'.$this->platform_url.'/api/video/setDetails/');

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$data       = array(
						'sig' 		=> $sig,
						'datas'  	=> $datas,
						'token' 	=> $this->token,
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passÃ© correctement
		$response	= curl_exec($ch);

		curl_close($ch);

		return $response;
	}

	function uploadKAD($file_path, $kad_datas)
	{
		$response  = '';

		$ch         = curl_init('http://'.$this->upload_url.'/api/uploadKAD/index.php');

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$data       = array(
						'file' 		=> '@'.$file_path,
						'kad_data'  => $kad_datas,
						'token' 	=> $this->token,
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passÃ© correctement
		$response	= curl_exec($ch);

		curl_close($ch);

		return $response;
	}

	function addMixVideo($sound_sig, $webcam_url, $xml_data, $site_url, $background_sig='')
	{
		$response  = '';

		$ch         = curl_init('http://'.$this->upload_url.'/api/addMixedVideo/index.php');

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$data       = array(
						'webcam_url'		=> $webcam_url,
						'video_data'		=> $xml_data,
						'token' 			=> $this->token,
						'sound_sig'			=> $sound_sig,
						'site_url' 			=> $site_url,
						'background_sig' 	=> $background_sig,
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passÃ© correctement
		$response	= curl_exec($ch);

		curl_close($ch);

		return $response;
	}


	function prepareUpload($md5, $xml_data, $token, $site_url)
	{
		$response  = '';

		$ch         = curl_init('http://'.$site_url.'/api/upload/prepareUpload/index.php');

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$myHeader = array(
			    "Expect:",
		);
	 	curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);

		$data       = array(
						'md5' 		=> $md5,
						'xml_data' 	=> $xml_data,
						'token' 	=> $this->token,
						'site_url' 	=> $site_url
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passÃ© correctement
		$response	= curl_exec($ch);

		curl_close($ch);

		return $response;
	}

	function uploadPreparedVideo($file, $site_url, $upload_token)
	{
		$response  = '';

		$ch         = curl_init('http://'.$this->upload_url.'/api/uploadPreparedVideo/index.php');

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$myHeader = array(
			    "Expect:",
		);
	 	curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);

		$data       = array(
						'file' 			=> '@'.$file,
						'upload_token' 	=> $upload_token,
						'site_url' 		=> $site_url
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passÃ© correctement
		$response	= curl_exec($ch);

		curl_close($ch);

		return $response;
	}

	function createKAD($kad_datas, $image_id, $plateform_url)
	{
		$response  = '';

		$ch         = curl_init('http://'.$plateform_url.'/api/kad/create/');

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$myHeader = array(
			    "Expect:",
		);
	 	curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);

		$data       = array(
						'datas' 		=> $kad_datas,
						'token' 		=> $this->token,
						'image_id'      => $image_id
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passÃ© correctement
		$response	= curl_exec($ch);

		curl_close($ch);

		return $response;
	}

	function uploadURL($url, $xml_data, $site_url, $upload_key='')
	{
		$response  = '';

		$ch         = curl_init('http://'.$this->upload_url.'/api/addVideoFromURL/index.php?X-Progress-ID='.$upload_key);

		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$data       = array(
						'url' 		=> $url,
						'xml_data' 	=> $xml_data,
						'token' 	=> $this->token,
						'site_url' 	=> $site_url
						);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

		// Contient le sig si tout c'est passÃ© correctement
		$response	= curl_exec($ch);

		curl_close($ch);

		return $response;
	}


	function createXMLData($datas)
	{
		$xml = new XMLWriter();
		$xml->openMemory();
		$xml->setIndentString("\t");
		$xml->setIndent(true);
		$xml->startDocument('1.0', 'utf-8');

		$xml->startElement('kewego_call');
		$xml->startElement('params');
		foreach ($datas as $key => $value)
		{
			$xml->startElement($key);
			if (is_array($value))
			{
				foreach($value as $subkey => $subvalue)
			    {
					if (is_numeric($subkey))
					{
						foreach($subvalue as $balise => $balise_value)
						{
							$xml->startElement($balise);
					        $xml->writeRaw($balise_value);
					        $xml->endElement();
						}
					}
					else
					{
						$xml->startElement($subkey);
				        $xml->writeRaw($subvalue);
				        $xml->endElement();
					}
				}
			}
			else
			{
		        $xml->writeRaw($value);
			}
	        $xml->endElement();
		}
		$xml->endElement();
		$xml->endElement();
		return $xml->outputMemory(false);
	}

	function data_encode($data, $keyprefix = "", $keypostfix = "") {
	  assert( is_array($data) );
	  $vars=null;
	  foreach($data as $key=>$value) {
	    if(is_array($value)) $vars .= data_encode($value, $keyprefix.$key.$keypostfix.urlencode("["), urlencode("]"));
	    else $vars .= $keyprefix.$key.$keypostfix."=".urlencode($value)."&";
	  }
	  return $vars;
	}

	function getToken()
	{
	    return $this->token;
	}

	function returnAppToken()
	{
	    return $this->appToken;
	}
}

?>